#{extends 'main.html' /}
#{set title:'首页设置' /}

<script type="text/javascript" src="/public/sui/js/lrz.all.bundle.js"></script>
<script type="text/javascript" src="/public/sui/js/uploadImg2.js"></script>
  <body>
    <div class="page-group home" >
    <div class="page">
        <header class="bar bar-nav">
             <a href="javascript:history.go(-1);" class="icon icon-left pull-left"></a>
             <h1 class="title">添加banner</h1>
        </header>
      <section class="banner_add content scroll_y">
          <div class="content-block">
              <input type="file" accept="image/*" id="cameraInput" name="cameraInput"
                     style="display:none;"/>
              <input type="file" accept="image/*" id="cameraInput_andriod" name="cameraInput_andriod"
                     style="display:none;"/>
              <dl>

                  #{if promotionActivity != null}
                      <input type="hidden" id="promotionActivityId" value="${promotionActivity?.id}"/>
                      <dd class="img">
                      <a href="#">
                          <i class="iconfont">&#xe6d3;</i>
                      </a>
                      <img src="${promotionActivity?.mainImg}">
                      <input type="hidden" class="imgs" value="${promotionActivity?.mainImg}"/>
                  </dd>
                  #{/if}
                      <dd id="idBtnAdd" class="add create-actions ">
                          <img src="/public/sui/images/btn_add.png">
                      </dd>


              </dl>

              <a href="#" class="button button-big button-fill button-success open-about" onclick="saveBlock()">保存</a>
              <p>注：最多只能上传一张图片</p>
          </div>
       </section>

    #{include 'footer.html'/}
    </div>
    <script>
        $.init();
    </script>

    <script type="text/javascript">

        $(document).ready(function () {
            //遍历垃圾桶图标,并绑定删除
            $(".iconfont").each(function (i) {
                $(this).bind("click", function () {
                    $(this).parent().parent().remove();
                    if ($(".imgs").length > 1) {
                        $("#idBtnAdd").hide();
                    } else {
                        $("#idBtnAdd").show();
                    }
                })
            });
            if ($(".imgs").length > 1) {
                $("#idBtnAdd").hide();
            } else {
                $("#idBtnAdd").show();
            }
        });

        function saveBlock() {

            var imgList = [];
            $(".imgs").each(function (i) {
                if (i == $(".imgs").length - 1) {
                    return;
                }
                if ($(this).val() != undefined && $(this).val() != "") {
                    imgList.push($(this).val());
                }
            });
            if(imgList.length == 0){
                $.toast('请上传活动图片!');
                return ;
            }
            if(imgList.length >1){
                $.toast('目前只能上传一张图片!');
                return ;
            }
            var imageFile = imgList[0];
            var promotionActivityId = $('#promotionActivityId').val();
            $.getJSON('/index/promotion/save', {promotionActivityId :promotionActivityId ,imageFile: imageFile}, function (data) {
                if (data.success) {
                    $.toast('保存成功!');
                    window.location.href = data.routerr;
                } else {
                    $.toast(data.msg);
                }
            })
        }

    </script>
    <div id="img_tmp" style="display: none;">
        <dd class="img">
            <a href="#">
                <i class="iconfont">&#xe6d3;</i>
            </a>
            <img src="" id="img_src"/>
            <input type="hidden" class="imgs" id="img_src_hidden" value="imgSrc"/>
        </dd>
    </div>
    </div>

  </body>
